(function($){
    var _lookup = {currentGroup:"", suffix:"", $target:null, pk:"id"};
    var _util = {

        lookupField: function(){
            var fieldGroup = [];
            var groups = _lookup.currentGroup.split(",") ;
            $.each(groups,function (index,value) {
                var field ={}
                if(value.indexOf(":") > 0){
                    var  fields = value.split(":");
                    field[fields[0]] = fields[1];
                    fieldGroup.push(field);
                }else{
                    field[value] = value;
                    fieldGroup.push({value:value});
                }
            })
            return fieldGroup;
        }
    };

    $.extend({
        bringBackSuggest: function(args){
            var $box = _lookup['$target'].parents(".modal:first") , fieldGroup = _util.lookupField();
            if($box.length == 0 ){
                var $box = _lookup['$target'].parents(".page-container")
            }
            $.each(fieldGroup,function(index,field){
                for (var key in field) {
                   var  $input = $box.find(':input[name="'+key+'"]'), value = field[key];
                    if ($input) {
                        $input.val(args[value]);
                        $input.triggerHandler('change');
                    }
                }
            });
        },
        bringBack: function(args){
            $.bringBackSuggest(args);
            Bolt.dialog('closeCurrent');
        },
        getSelectedIds: function(selectedIds, targetType){
            var ids = "";
            var $box = targetType == "dialog" ? $.pdialog.getCurrent() : navTab.getCurrentPanel();
            $box.find("input:checked").filter("[name='"+selectedIds+"']").each(function(i){
                var val = $(this).val();
                ids += i==0 ? val : ","+val;
            });
            return ids;
        }
    });

    $.fn.extend({
        lookup: function(){
            return this.each(function(){
                $(this).unbind('click').click(function(event){

                   var   $this = $(this), data = $this.attr('data-options').toJson() ;


                    _lookup = $.extend(_lookup, {
                        currentGroup: $this.attr("lookupGroup") || "",
                        suffix: $this.attr("suffix") || "",
                        $target: $this,
                        pk: $this.attr("lookupPk") || "id"
                    });

                    var url = decodeURI($this.attr("href")).replaceTmById($(event.target));
                    if(url){
                        data.url = url;
                    }
                    if(data && !data.title){
                        data.title = "选择"
                    }
                    if(data && !data.modalSize){
                        data.modalSize = "modal-sm"
                    }

                    $this.dialog(data)
                    return false;
                });
            });
        }
    });
})(jQuery);

